const path = require('path');
const webpack = require('webpack');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const glob = require('glob');

const FriendlyErrorsWebpackPlugin = require("friendly-errors-webpack-plugin");

const setMPA = ()=>{
  const entry = {};
  const htmlWebpackPlugins = [];

  const entryFiles = glob.sync(path.join(__dirname, './src/*/index.js'));
  Object.keys(entryFiles)
      .map((index)=>{
        const entryFile = entryFiles[index]
      });
  entryFiles.map((entryFile)=>{
    let match = entryFile.match(/src\/(.*)\/index\.js$/);
    let pageName = match[1];
    entry[pageName] = entryFile;
    htmlWebpackPlugins.push(
        new HtmlWebpackPlugin({
          template: path.join(__dirname, `src/${pageName}/index.html`),
          filename: `${pageName}.html`,
          chunks: [pageName],
          inject: true,
          minify: {
            html5: true,
            collapseWhitespace: true,
            preserveLineBreaks: false,
            minifyCSS: true,
            minifyJS: true,
            removeComments: false
          }
        })
    )
  });

  return {
    entry,
    htmlWebpackPlugins
  }
};

const {entry , htmlWebpackPlugins} = setMPA();

module.exports = {
  entry,
  output: {
    path: path.join(__dirname, 'dist'),
    filename: "[name].js"
  },
  mode: "production",
  module: {
    rules: [
      {
        test: /\.js$/,
        use: 'babel-loader'
      },
      {
        test: /\.css$/,
        use: [
            'style-loader',
            'css-loader'
        ]
      },
      {
        test: /\.less$/,
        use: [
          'style-loader',
          'css-loader',
          'less-loader'
        ]
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/,
        loader: 'file-loader',
        options: {
        }
      },
      {
        test: /\.(jpg|png|svg|gif)$/,
        use: [{
          loader: "url-loader",
          options: {
            limit: 10240
          }
        }]
      },
    ]
  },
  plugins: [
      new webpack.HotModuleReplacementPlugin(),
      new CleanWebpackPlugin(),
      new FriendlyErrorsWebpackPlugin()
  ].concat(htmlWebpackPlugins),
  devServer: {
    contentBase: './dist',
    hot: true,
    stats: 'errors-only'
  },
  devtool: 'sourcemap'
};
